package com.zl.learning.reflect;

import org.junit.Test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class BeanInfoTest {
    @Test
    public void test1() {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor descriptor : descriptors) {
                System.out.println(descriptor.getDisplayName()+"--"+descriptor.getReadMethod()+"---"+descriptor.getWriteMethod()+"----"+descriptor.getPropertyType());
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
    }

    public static class Test1 {
        public static void main(String[] args) {
            try {
                BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
                Stream.of(beanInfo.getPropertyDescriptors()).forEach(r-> {
                    try {
                        System.out.println(r.getReadMethod().getName());
                        System.out.println(r.getWriteMethod().getName());
                        System.out.println(r.getDisplayName());
                        System.out.println(r.getName());
                        System.out.println(r.getPropertyType());
                        System.out.println("--------------------------------");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Test2 {
        public static void main(String[] args) {
            try {
                BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
                User user = new User();
                PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
                for (PropertyDescriptor descriptor:descriptors){
                    String name = descriptor.getName();
                    System.out.println(name);
                    if(name.equals("username")){
                        Method method = descriptor.getWriteMethod();
                        method.invoke(user,"zhangsan");
                    }
                }
                System.out.println(user);
//                OutputStream out  = Files.newOutputStream(Paths.get("result.xml"), StandardOpenOption.CREATE_NEW);
//                XMLEncoder xmlEncoder = new XMLEncoder(out, StandardCharsets.UTF_8.name(),true,0);
//                xmlEncoder.writeObject(user);
//                xmlEncoder.flush();
//                out.close();
//                xmlEncoder.close();
            } catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Test3 {
        public static void main(String[] args) {
            System.out.println(Paths.get("result.xml").toAbsolutePath());
        }
    }
}
